package com.example.post.controller;

import com.example.post.model.Blog;
import com.example.post.model.Bookmark;
import com.example.post.model.Result;
import com.example.post.model.User;
import com.example.post.services.impl.BookmarkServiceImpl;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class BookmarkController{

    @Autowired
    BookmarkServiceImpl bookmarkService;

    @PostMapping(value = "/api/addBookmark")
    public Result addBookmark(HttpSession session, @RequestBody Bookmark bookmark) {
        User user = (User) session.getAttribute("user");
        bookmark.setUserId(user.getId());
        bookmark.setCreateDate(LocalDateTime.now());
        bookmarkService.addBookmark(bookmark);
        return new Result(200);
    }
    @PostMapping(value = "/api/deleteBookmark/{blogId}")
    public Result deleteBookmark(@PathVariable int blogId) {
        bookmarkService.deleteBookmark(blogId);
        return new Result(200);
    }
    @GetMapping(value = "/api/checkBookmark/{blogId}")
    public Result checkBookmark(@PathVariable int blogId) {
        System.out.println(blogId);
        return bookmarkService.checkBookmark(blogId);
    }
    @GetMapping(value = "/api/bookmarks")
    public List<Blog> getBookmarks(HttpSession session) {
        User user = (User) session.getAttribute("user");

        return bookmarkService.getBookmarksByUserId(user.getId());

    }
}
